
package com.cgy.controller;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.cgy.entity.Computer;
import com.cgy.entity.ConsumeLogs;
import com.cgy.entity.ResStatistics;
import com.cgy.entity.ZiYuan;
import com.cgy.repository.ConsumeLogsRepository;
import com.cgy.service.ComputerService;
import com.cgy.service.ConsumeLogsService;
import com.cgy.service.ResStatisticsService;

import util.LayuiJsonUtils;

@RestController
@RequestMapping("consumeLogs")
@CrossOrigin
public class ConsumeLogsController {
	//打卡
	@Autowired
	private ConsumeLogsService ConsumeLogsService;
	@Autowired
	private ConsumeLogsRepository cm;
 	//统计
	@Autowired
	private ResStatisticsService resstaservice;	
	@GetMapping
	public LayuiJsonUtils<ConsumeLogs> findAllConsumeLogs(LayuiJsonUtils<ConsumeLogs> lj,ConsumeLogs c){
		Pageable pageable = PageRequest.of(lj.getPage(), lj.getLimit());
		Page<ConsumeLogs> ConsumeLogs = ConsumeLogsService.findAllConsumeLogs(c, pageable);
		lj.setData(ConsumeLogs.getContent());
		lj.setCount(ConsumeLogs.getTotalElements());
		return lj;
	}
	
	//添加
	@PostMapping
	public ConsumeLogs addConsumeLogs(ConsumeLogs c,Integer zid,Integer cid,ResStatistics ressta,Computer com) {
		ZiYuan z = new ZiYuan();
		z.setZid(zid);
		c.setZiYuan(z);
		Computer computer = new Computer();
		computer.setCid(cid); 
		c.setComputer(computer);
		c.setLStatus(1);
		Date date=new Date();
		c.setLInTime(String.valueOf(new java.sql.Date(date.getTime())));
		com.setCstatus(1);
		
		return ConsumeLogsService.addAndUpdateConsumeLogs(c);
	}
	//修改
	@PutMapping("upConsumeLogsstatic")
	public ConsumeLogs updateConsumeLogs(ConsumeLogs c,Integer zid,Integer cid,Computer com) {
		ZiYuan z = new ZiYuan();
		z.setZid(zid);
		c.setZiYuan(z); 
		Computer computer = new Computer();
		computer.setCid(cid); 
		c.setComputer(computer);
		c.setLStatus(0); 
		Date date=new Date();
		c.setLOutTime(String.valueOf(new java.sql.Date(date.getTime())));
		com.setCstatus(0); 
		return ConsumeLogsService.addAndUpdateConsumeLogs(c);
	}
	@DeleteMapping("{lid}")
	public Integer deleteConsumeLogs(@PathVariable("lid") Integer lid) {
		return ConsumeLogsService.deleteConsumeLogs(lid);
	}
	/*
	 * @CrossOrigin
	 * 
	 * @RequestMapping("shuaka") public Integer saveCon(ConsumeLogs c,String no) {
	 * SimpleDateFormat sd=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String
	 * in_time =sd.format(new Date())+""; String out_time = sd.format(new
	 * Date())+""; System.out.println(in_time); if(c.getLStatus().equals("1")) {
	 * c.setLInTime(in_time); c.setLOutTime(out_time); return
	 * cm.saveAndFlush(c)!=null?1:0; }else if(c.getLStatus().equals("0")) { return
	 * cm.saveAndFlush(c)!=null?1:0; } return 0; }
	 */
	}


 
